package com.cbnu.SmartCampusLife;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;

public class OAuthSetup {

  public static void main(String args[]) throws Exception {
     
      Twitter twitter = new TwitterFactory().getInstance();

      twitter.setOAuthConsumer("43CdpWZ56QUiqKEDFy3hg", "y1BbrgIlIZsGqP1AiFAYoubcy2FgcmUkglE7f2wxNo");
      RequestToken requestToken = twitter.getOAuthRequestToken();      AccessToken accessToken = null;
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      while (null == accessToken) {
        System.out.println("Open the following URL and grant access to your account:");
        System.out.println(requestToken.getAuthorizationURL());
        System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
        String pin = br.readLine();
        try{
          
          
           if(pin.length() > 0){
             accessToken = twitter.getOAuthAccessToken(requestToken, pin);
           }else{
             accessToken = twitter.getOAuthAccessToken();
           }
        } catch (TwitterException te) {
          if(401 == te.getStatusCode()){
            System.out.println("Unable to get the access token.");
          }else{
            te.printStackTrace();
          }
        }
      }
      System.out.println(twitter.verifyCredentials().getId());
      System.out.println("token : " + accessToken.getToken());
      System.out.println("tokenSecret : " + accessToken.getTokenSecret());  
    }
}
